package com.ctg.payroll2.action.payment;

import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;

import com.ctg.payroll2.action.BaseAction;
import com.ctg.payroll2.bean.filter.PayClassFilter;
import com.ctg.payroll2.bean.po.PayClass;
import com.ctg.payroll2.bean.po.payment.PayClassitem;
import com.ctg.payroll2.service.payment.PayClassService;
import com.ctg.payroll2.service.payment.PayClazzItemService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class PayClassAction extends BaseAction {
	@Override
	protected void createEntity() {
		if (payClass!=null) {
			payClass.setCompany_code(currentUser.getCompanyCode());
			if (payClass.getPayrule().equals("")) {
				payClass.setPayrule(Constants.PAY_DAY_RULE);
			}
		}
		Long pay_class_id=payClassService.insertPayClass(payClass);
		PayClassitem temp=new PayClassitem();
		temp.setIn_tax("0");
		temp.setData_come("1");
		temp.setIs_add("1");
		temp.setBonus_option("-1");
		temp.setIs_fixed("0");
		temp.setMonth("0");
		temp.setPrecision("2");
		temp.setIs_limited("0");
		temp.setIs_valid("1");
		temp.setIs_display("0");
		temp.setMemo("0");
		temp.setCalculate_order("0");
		temp.setDisplay_order("0");
		temp.setName("本月补足");
		temp.setZero_nextmonth("0");
		temp.setDatanode(0);
		temp.setSegment_order(23);
		temp.setPay_class_id(pay_class_id.toString());
		temp.setCompany_code(currentUser.getCompanyCode());
		temp.setCreator_code(currentUser.getEmployeeCsCode());
		temp.setIs_subsection("0");
		Long pay_classitem_id=payClazzItemService.insertPayItem(temp);
		temp.setSegment1(pay_classitem_id.toString());
		payClazzItemService.modifyPayItem(temp);
		temp.setPay_classitem_id(null);
		temp.setSegment_order(24);
		temp.setName("下月扣款");
		pay_classitem_id=payClazzItemService.insertPayItem(temp);
		temp.setSegment1(pay_classitem_id.toString());
		payClazzItemService.modifyPayItem(temp);
	}

	@Override
	protected void deleteEntity() {
		if(payClassService.fakeDeletePayClazzByIds(Long.parseLong(deleteIds)))
			success = true;
	}

	@Override
	protected void queryEntity() {
		filter = autoSetfilter(filter);
		filter.setIsValid(Constants.VALID_T);
		filter.setCompanyCode(currentUser.getCompanyCode());
		result = payClassService.queryPayClassPageByFilter(filter);
	}

	@Override
	protected void retrieveEntity() {
		payClass = payClassService.retrieveEntity(payClass.getPay_class_id());
	}

	@Override
	protected void updateEntity() {
		payClass.setModifier_code(currentUser.getEmployeeCsCode());
		payClass.setCompany_chinese_code(currentUser.getCompanyName());
		payClass.setCompany_code(currentUser.getCompanyCode());
		if (StringUtils.isEmpty(payClass.getPayrule())) {
			payClass.setPayrule(Constants.PAY_DAY_RULE);
		}
		try {
			payClassService.updatePayClazz(payClass);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void exportXLS() {}
	
	@Override
	protected void commonOperations() {}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/******************************************
	 * 
	 * getter, setter 方法
	 * 
	 ******************************************/
	private static final long serialVersionUID = 1958783025163387280L;
	private String deleteIds;
	private boolean success = false;
	private Page result; 
	/**
	 * 薪资类别管理查询过滤条件
	 */
	private PayClassFilter filter;
	private PayClass payClass;
	/**
	 * 使用的service，用spring注入
	 */
	private PayClassService payClassService;
	private PayClazzItemService payClazzItemService;
	private PayClassFilter autoSetfilter(PayClassFilter filter){
		if (filter==null) {
			filter = new PayClassFilter();
		}
		filter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		return filter;
	}
	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	/**
	 * @return 返回filter
	 */
	public PayClassFilter getFilter() {
		return filter;
	}

	/**
	 * @param filter 设置filter
	 */
	public void setFilter(PayClassFilter filter) {
		this.filter = filter;
	}

	/**
	 * @param payClassService 设置payClassService
	 */
	public void setPayClassService(PayClassService payClassService) {
		this.payClassService = payClassService;
	}

	/**
	 * @return 返回payClass
	 */
	public PayClass getPayClass() {
		return payClass;
	}

	/**
	 * @param payClass 设置payClass
	 */
	public void setPayClass(PayClass payClass) {
		this.payClass = payClass;
	}

	/**
	 * @return 返回deleteIds
	 */
	public String getDeleteIds() {
		return deleteIds;
	}

	/**
	 * @param deleteIds 设置deleteIds
	 */
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}

	/**
	 * @return 返回success
	 */
	public boolean isSuccess() {
		return success;
	}

	/**
	 * @param success 设置success
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setPayClazzItemService(PayClazzItemService payClazzItemService) {
		this.payClazzItemService = payClazzItemService;
	}
}
